// const sql = require('mssql');

var {Sequelize,DataTypes} = require("sequelize");
let { dboption } = require("../conf");

const sequelize = new Sequelize(dboption);

var consproject = require('../models/base_consproject')(sequelize, DataTypes);
var base_conpermit_info = require('../models/base_conpermit_info')(sequelize, DataTypes);

(async () => {
    try {
        // make sure that any items are correctly URL encoded in the connection string
        // await sql.connect('Server=60.211.159.133,8090;Database=zj_cycle;User Id=sa;Password=Hxy971123;trustServerCertificate=true;')
        // const result = await sql.query `select top 1 * from base_conpermit_project`;
        // result.recordset.forEach(row => {
        // })
        // const res = await model.findAll({where:{id:{ [Op.lte]: 2}}});
        const res = await consproject.findAll();
        res.forEach(async row => {
            // row['施工许可系统中查询证号'] = row['施工许可系统中查询证号'].trim();
            // await row.save();
            let data = row.toJSON();

            let codes = data['施工许可系统中查询证号']?.replace(/；/g,';').split(';');
            codes?.forEach(async code=>{
                // await sequelize.query(`insert into baserel_consproject_conpermit values(${data.id},'${code}')`);
                await base_conpermit_info.update({ id_consproject:data.id  }, {
                    where: {
                        prjpwemits_num: code.trim()
                    }
                });
            })
        });

    } catch (err) {
        console.log('异常', err)
    }
})()